//
//  JHSLeftBarViewController.m
//  JHSSweep
//
//  Created by YanYaxing on 2017/3/23.
//  Copyright © 2017年 JHDS. All rights reserved.
//

#import "JHSLeftBarViewController.h"
#import "Masonry.h"
#import "JHSLogInDetailController.h"
#import "JHSUserStatus.h"
#import "JHSDoctorApplyViewController.h"
#import "JHSSetAndHelpViewController.h"
#import "JHSFollowDoctorController.h"
#import "JHSUsersDetailViewController.h"

//功能列表枚举
typedef enum {
    
    JHSFunctionFamousDoctorApply = 0,//名医申请
    
    JHSFunctionCaseManage,//病例管理
    
    JHSFunctionAttentionDoctor, //关注医生
    
    JHSFunctionSetAndHelp  //设置与帮助
    
}JHSFunction;//枚举名称

@interface JHSLeftBarViewController () <UITableViewDelegate,UITableViewDataSource,UIImagePickerControllerDelegate,UINavigationControllerDelegate,UIGestureRecognizerDelegate>


@property (nonatomic, weak) UIButton *iconBtn;

@end

static NSString *CELLID = @"CellID";


@implementation JHSLeftBarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor colorWithRed:116 / 255.0 green:191 / 255.0 blue: 159 / 255.0 alpha:1.0];

    
    if ([JHSUserStatus sharedUserAccount].isLogin==1) {
        NSLog(@"你登录了");

    }

    [self setupUI];
    
}



-(void)setupUI{
    //MARK：登录后显示的UI
    if ([JHSUserStatus sharedUserAccount].isLogin==0) {
        NSLog(@"已登录");
        //tableView
        UITableView *tableView=[UITableView new];
        
        tableView.backgroundColor = JHSMainColor;
        
        tableView.contentInset = UIEdgeInsetsMake(80, 0, 0, 0);
        
        tableView.sectionIndexBackgroundColor = [UIColor colorWithRed:116 / 255.0 green:191 / 255.0 blue: 159 / 255.0 alpha:1.0];
        
        
        [self.view addSubview:tableView];
        
        [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.offset(0);
            make.left.right.offset(0);
            make.bottom.offset(0);
        }];
        
        _tableView=tableView;
        
        UIView *view = [UIView new];
        view.backgroundColor = [UIColor colorWithRed:116 / 255.0 green:191 / 255.0 blue: 159 / 255.0 alpha:1.0];
        tableView.tableFooterView = view;
        
        
        //头部view
        UIView *headView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 200, 100)];
        
        headView.backgroundColor=JHSMainColor;

        UITapGestureRecognizer *tapGestureRecongnizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickedHeadView)];
        [headView addGestureRecognizer:tapGestureRecongnizer];
        
        //用户图标
        UIButton *iconButton= [UIButton new];
        _iconBtn = iconButton;
        
        [iconButton setImage:[UIImage imageNamed:@"doctor_defaultphoto_male"] forState:UIControlStateNormal];
        
        //线宽颜色
        iconButton.layer.borderColor = [UIColor whiteColor].CGColor;
        iconButton.layer.cornerRadius = 30;
        iconButton.layer.masksToBounds = YES;
        
        iconButton.layer.borderWidth = 2;
        
        [headView addSubview:iconButton];
        
        [iconButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.left.offset(16);
            make.height.width.offset(60);
        }];
        
        //MARK:给头像添加点击事件
        [iconButton addTarget:self action:@selector(chooseImage:) forControlEvents:UIControlEventTouchUpInside];
        
        
        UILabel *nameLabel = [UILabel new];
        nameLabel.text=@"张三";
        nameLabel.textColor = [UIColor whiteColor];
        
        [headView addSubview:nameLabel];
        [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(iconButton.mas_top).offset(0);
            make.left.equalTo(iconButton.mas_right).offset(10);
            
        }];
        
        UILabel *phoneLabel = [UILabel new];
        phoneLabel.text=@"13888138138";
        phoneLabel.textColor = [UIColor whiteColor];
        
        [headView addSubview:phoneLabel];
        [phoneLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(iconButton.mas_bottom).offset(0);
            make.left.equalTo(nameLabel.mas_left).offset(0);
            
        }];
        
        [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:CELLID];
        tableView.dataSource=self;
        tableView.delegate=self;
        //添加到tableView
        tableView.tableHeaderView=headView;
        
        
        
        

        
    }
    //MARK: 未登陆显示的UI
    else{
        NSLog(@"未登陆");
    //MARK:注册按钮
    UIButton *registerBtn=[[UIButton alloc]init];
    [self.view addSubview:registerBtn];
    [registerBtn setTitle:@"注册" forState:UIControlStateNormal];
    [registerBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(60);
        make.left.offset(30);
    }];
    
    //MARK:默认头像
    UIImageView *imageView=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"doctor_defaultphoto_male"]];
    [self.view addSubview:imageView];
    
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(registerBtn.mas_top);
        make.left.equalTo(registerBtn.mas_right).offset(18);
        make.width.height.offset(50);
    }];

    //MARK:登录按钮
    UIButton *loginBtn=[[UIButton alloc]init];
    [self.view addSubview:loginBtn];
    [loginBtn setTitle:@"登录" forState:UIControlStateNormal];
    [loginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(registerBtn.mas_top);
        make.left.equalTo(imageView.mas_right).offset(18);
    }];
    [loginBtn addTarget:self action:@selector(clickedLoginBtn) forControlEvents:UIControlEventTouchUpInside];
    }
}


#pragma mark - 保存图片至沙盒
- (void) saveImage:(UIImage *)currentImage withName:(NSString *)imageName
{
    NSData *imageData = UIImageJPEGRepresentation(currentImage, 0.5);
    // 获取沙盒目录
    NSString *fullPath = [[NSHomeDirectory()stringByAppendingPathComponent:@"Documents"]stringByAppendingPathComponent:imageName];
    
    // 将图片写入文件
    [imageData writeToFile:fullPath atomically:NO];
    
}


#pragma mark - image picker delegte
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    [picker dismissViewControllerAnimated:YES completion:^{}];
    
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
    
    [self saveImage:image withName:@"currentImage.png"];
    
    NSString *fullPath = [[NSHomeDirectory()stringByAppendingPathComponent:@"Documents"]stringByAppendingPathComponent:@"currentImage.png"];
    
    UIImage *savedImage = [[UIImage alloc] initWithContentsOfFile:fullPath];

    [_iconBtn setImage:savedImage forState:UIControlStateNormal];
    
}


- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [self dismissViewControllerAnimated:YES completion:^{}];
}


#pragma mark - actionsheet delegate
-(void) actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
        // 跳转到相机或相册页面
        UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
        
        imagePickerController.delegate = self;
        
        imagePickerController.allowsEditing = YES;
    
        [self presentViewController:imagePickerController animated:YES completion:^{}];

}

//MARK:11- 点击头像更换图片
- (void)chooseImage:(UITapGestureRecognizer *)sender
{
    UIActionSheet *sheet;
    
    // 判断是否支持相机
    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
    {
        sheet = [[UIActionSheet alloc] initWithTitle:@"选择"delegate:self cancelButtonTitle:nil destructiveButtonTitle:@"取消" otherButtonTitles:@"拍照",@"从相册选择",nil];
    }
    else {
        sheet = [[UIActionSheet alloc] initWithTitle:@"选择"delegate:self cancelButtonTitle:nil destructiveButtonTitle:@"取消" otherButtonTitles:@"从相册选择",nil];
    }
    sheet.tag = 255;
    
    [sheet showInView:self.view];
}


//MARK: 10- 监听了登录按钮
-(void)clickedLoginBtn{
    
    NSLog(@"点击了登录按钮");
    JHSLogInDetailController *vc=[JHSLogInDetailController new];
    vc.hidesBottomBarWhenPushed=YES;
    self.hidesBottomBarWhenPushed=NO;
    [self goToViewController:vc andTitle:@"登录"];
}

#pragma mark - tableView数据源
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 5;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CELLID forIndexPath:indexPath];
    cell.textColor = [UIColor whiteColor];
    cell.backgroundColor =JHSMainColor;
    
    //隐藏分割线
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    //取消cell的选中状态
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    

    switch (indexPath.row) {
        case JHSFunctionFamousDoctorApply:{
            cell.textLabel.text=@"名医申请";
            UIImageView *imgV = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"名医申请"]];
            [cell.contentView addSubview:imgV];
            
            [imgV mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.offset(20);
                make.centerY.offset(0);
                make.size.mas_offset(CGSizeMake(25, 25));
            }];
            
            [cell.textLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.offset(50);
                make.centerY.offset(0);
            }];
            
        }
            break;
        case JHSFunctionCaseManage:{
            cell.textLabel.text=@"病例管理";
        
            UIImageView *imgV = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"病例管理"]];
            [cell.contentView addSubview:imgV];
            
            [imgV mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.offset(20);
                make.centerY.offset(0);
                make.size.mas_offset(CGSizeMake(25, 25));
            }];
            
            [cell.textLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                
                make.left.offset(50);
                make.centerY.offset(0);
            }];
        
        }
            break;
        case JHSFunctionAttentionDoctor:{
            cell.textLabel.text=@"关注医生";
            
            UIImageView *imgV = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"关注医生"]];
            [cell.contentView addSubview:imgV];
            
            [imgV mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.offset(20);
                make.centerY.offset(0);
                make.size.mas_offset(CGSizeMake(25, 25));
            }];
            
            [cell.textLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                
                make.left.offset(50);
                make.centerY.offset(0);
            }];
            
        }
            break;
        case JHSFunctionSetAndHelp:{
            cell.textLabel.text=@"设置与帮助";
        
            UIImageView *imgV = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"设置与帮助"]];
            [cell.contentView addSubview:imgV];
            
            [imgV mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.offset(20);
                make.centerY.offset(0);
                make.size.mas_offset(CGSizeMake(25, 25));
            }];
            
            [cell.textLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                
                make.left.offset(50);
                make.centerY.offset(0);
            }];
        
        }
            break;
        default:
            break;
    }
    
    //4.返回cell
    return cell;
}


//改变bar的样式
- (UIStatusBarStyle)preferredStatusBarStyle{
    return UIStatusBarStyleLightContent;

}


#pragma mark - 点击头像跳转到用户详细信息界面
-(void)clickedHeadView{
    
//    JHSUsersDetailViewController *userInfoView = [[JHSUsersDetailViewController alloc]init];
    
    
    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"JHSUsersDetailViewController" bundle:nil];
    
    JHSUsersDetailViewController *userInfoVC = [sb instantiateViewControllerWithIdentifier:@"userInfo"];
    
    userInfoVC.hidesBottomBarWhenPushed=YES;
    self.hidesBottomBarWhenPushed=NO;
    

    [self goToViewController:userInfoVC andTitle:@"用户信息"];
    
}


#pragma mark - 跳转VC的方法
- (void)goToViewController:(UIViewController *)viewController andTitle:(NSString *)title{
    viewController.title = title;
    UITabBarController *tab = (UITabBarController *)self.drawer.centerViewController;
    UINavigationController *nav = tab.selectedViewController;
    [nav popToRootViewControllerAnimated:NO];
    [self.drawer close];
    [nav pushViewController:viewController animated:NO];
}


#pragma mark - cell的点击事件
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSInteger currentRow=indexPath.row; //获取当前行的枚举
    JHSDoctorApplyViewController *vcOne=[JHSDoctorApplyViewController new];
//    JHSSetAndHelpViewController *vcTwo=[JHSSetAndHelpViewController new];
    JHSFollowDoctorController *vcThree=[JHSFollowDoctorController new];
    JHSSetAndHelpViewController *vcFour=[JHSSetAndHelpViewController new];
    
    
    switch (currentRow) {
        case JHSFunctionFamousDoctorApply:
            vcOne.hidesBottomBarWhenPushed=YES;
            self.hidesBottomBarWhenPushed=NO;
            [self goToViewController:vcOne andTitle:@"名医申请"];
            break;
        case JHSFunctionCaseManage:{
            UIStoryboard *sb = [UIStoryboard storyboardWithName:@"JHSSicknessManageController" bundle:nil];
            UIViewController *sickManageVC = [sb instantiateViewControllerWithIdentifier:@"sickManage"];
            sickManageVC.hidesBottomBarWhenPushed = YES;
            [self goToViewController:sickManageVC andTitle:@"病例管理"];
            
        }
            break;
        case JHSFunctionAttentionDoctor:
            vcThree.hidesBottomBarWhenPushed=YES;
            self.hidesBottomBarWhenPushed=NO;
            [self goToViewController:vcThree andTitle:@"关注医生"];
            break;
            
        case JHSFunctionSetAndHelp:
            vcFour.hidesBottomBarWhenPushed=YES;
            self.hidesBottomBarWhenPushed=NO;
            [self goToViewController:vcFour andTitle:@"设置与帮助"];
            break;
            
        default:
            break;
    }
    
}


@end
